<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller {
    public function index(){	

    	//如果直接进入购物车,没有传ID 则不会查询数据库
    	if (!empty($_POST)) {
    		$id=$_POST['id'];//商品的id
	    	$num=$_POST['num'];//商品的数量
	    	
	    	//查询商品的信息
	    	$model = D('goods');
    		$goods=$model-> Cartgoods($id);
    		$goods['num']=$num;
    		/*
				把所有加入购物车的商品的信息存放到session中
				[$id]区分每个商品的品种用他们自身的id分开
				直接进入购物车 就不会查询数据库,
			*/
    		$_SESSION['cart'][$id]=$goods;
    	}
    	// unset($_SESSION['cart']);
    	//计算商品的个数用于计算选择了多少个商品
    	$i=count($_SESSION['cart']);
    	// var_dump($_SESSION['cart']);
    	$total=0;
    	$this->assign('total',$total);
    	// dump($_SESSION['cart']);
    	//把所有加入购物的商品从session中遍历到页面上
    	$this->assign('session',$_SESSION['cart']);
    	$this->display();
    }

    //删除订单栏里面的内容
    public function delete(){
    	$id=$_GET['id'];
    	unset($_SESSION['cart'][$id]);
    	$this->redirect('Cart/index');
    }

    //清空购物篮
    public function emptys(){
    	unset($_SESSION['cart']);
    	$this->redirect('Cart/index');
    }
    //订单页面
    public function order(){
    	
    	//遍历个人收货地址
    	$model = M('address');
    	$data['uid']=$_SESSION['user']['id'];
    	$addre=$model->where($data)->select();
    	$this->assign('addre',$addre);

    	// var_dump($_SESSION['cart']);
    	$this->assign('session',$_SESSION['cart']);
    	$this->display('Cart/order');
    }

    //提交订单
    public function docart(){
    	$model_list = M('order_list');
    	$model_info = M('order_info');

    	//自动生成订单编号
    	$data['ordernumber'] ='TOP1'.date('Ymd',time()).uniqid();
    	//下单时间
    	$data['addtime'] = time(); 
    	//发货地址
    	$data['add_id'] = $_POST['add_id'];
    	//用户
    	$data['uid'] = $_SESSION['user']['id'];

    	//开启式事物预处理
    	$model_list->startTrans();
    	//插入数据库 得到数据id
    	$order_list = $model_list->data($data)->add();
    	if ($order_list) {

    		//得到订单表的id 再把商品循环插入到 订单详情表中
    		foreach ($_SESSION['cart'] as $key => $val) {

    			$dat['goods_id'] = $val['gid'];
    			$dat['order_id'] = $order_list;
    			$dat['num'] = $val['num'];
    			$order_info = $model_info->data($dat)->add();
    		}
    		if ($order_info) {
    			//如果2个都插入成功 则确认
    			$model_list->commit();

    			//添加成功清空购物篮
    			unset($_SESSION['cart']);
    			$this->success('购物成功');
    		}else{
    			//否则回滚
    			$model_list->rollback();
    			$this->error('结算失败');
    		}
    		
    	}
    	
    }

    //添加新的地址
    public function addres(){
    	foreach ($_POST as $key => $value) {
    		if (empty($value)) {
    			$this->error('表单不能为空','order',1);
    		}
    	}
    	$model = M('address');
    	$data['address'] = $_POST['address'];
    	$data['tel'] = $_POST['tel'];
    	$data['code'] = $_POST['code'];
    	$data['uid'] = $_SESSION['user']['id'];
    	var_dump($data);
    	// exit;
    	$addr  = $model->data($data)->add();
    	if ($addr) {
    		$this->success('添加成功');
    	}else{
    		$this->error('添加失败','order',1);
    	}
    }



//------------------------订单详情---------------

    public function orderdetails(){
       
        $model = M('order_list');
        $uid=$_SESSION['user']['id'];
        
        $res=$model->where('uid='.$uid)->select();

        //发送订单编号数据 前台遍历
        
         //循环查询每个订单下面的商品,然后加到该ID下面
        foreach ($res as $key => $v1) {

            $res[$key]['son']=$model->field('g.id gid,  g.name gname,  g.price gprice, g.describe gdesc,orlist.id orid,orlist.is_show ors,orlist.is_fu orf, gi.name giname,  orinfo.num orinfonum')->table('goods g, goods_img gi, order_list orlist, order_info orinfo')->where('g.id=orinfo.goods_id and gi.goods_id=g.id  and  orinfo.order_id=orlist.id and gi.is_face=1 and orlist.id='.$v1['id'])->select();
            
              // var_dump( $res[$key]['son']);
        }
        //发送订单商品 前台遍历
        $this->assign('res',$res);
        
        //var_dump($list);

        $this->display();
    }

//-------------------前台付款-----------------------
    public function fu(){

        $id = $_GET['id'];
        
        $model = M('order_list');

        $data['is_fu'] = 1;
        $fu = $model->where('id='.$id)->save($data);
        if ($fu) 
        {
          $this->redirect('Cart/orderdetails');
        }   
    }


//----------------------前台收货---------------------
    public function show(){

        $id = $_GET['id'];
        
        $model = M('order_list');

        $data['is_show'] = 2;
        $show = $model->where('id='.$id)->save($data);
        if ($show) 
        {
          $this->redirect('Cart/orderdetails');
        } 
    }


//---------------------删除订单----------------------
    public function del(){

        $id = $_GET['id'];

        $model = M('order_list');
        $res=$model->field('is_show,is_fu')->where('id='.$id)->find();

        //只有在客户没付款的时候才能取消订单  付款完成删除订单
       if ($res['is_show'] == 2 || $res['is_fu'] == 0) {
        $show = $model->where('id='.$id)->delete();
        if ($show) 
        {
            $this->redirect('Cart/orderdetails');
        } 
       }
    }   



 }
